/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jpm.jroguefx.levels;

import com.jpm.jroguefx.Constants;
import com.jpm.jroguefx.Player;
import com.jpm.jroguefx.types.TerrainType;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

public abstract class Area implements Constants {

    protected Tile[][] tileMap;
    private Tile playerTile = null;

    protected abstract int getXlimit();

    protected abstract int getYlimit();

    public synchronized void drawGround(GraphicsContext gc) {

        for (int y = 0; y < DRAW_SIZE; y++) {
            for (int x = 0; x < DRAW_SIZE; x++) {

                if ((x - y < 27) && (y - x < 23) && (x + y > 28) && (x + y < 68)) {

                    Image[] imageStack = getBaseTerrainImage(Player.getLocX() + x - (DRAW_SIZE / 2), Player.getLocY() + y - (DRAW_SIZE / 2));

                    if (imageStack == null) {
                        gc.drawImage(TerrainType.SEA.getImage(),
                                (x * (TILE_SIZE / 2)) + (y * (TILE_SIZE / 2)) - (14 * TILE_SIZE),
                                (y * (TILE_SIZE / 4)) - (x * (TILE_SIZE / 4)) + (6 * TILE_SIZE), TILE_SIZE, TILE_SIZE);
                    } else {
                        for (Image image : imageStack) {

                            if (image != null) {
                                gc.drawImage(image,
                                        (x * (TILE_SIZE / 2)) + (y * (TILE_SIZE / 2)) - (14 * TILE_SIZE),
                                        (y * (TILE_SIZE / 4)) - (x * (TILE_SIZE / 4)) + (6 * TILE_SIZE), TILE_SIZE, TILE_SIZE);
                            }
                        }
                    }
                }
            }
        }
    }

    public void drawPlayArea(GraphicsContext gc) {

        if (playerTile != null) {
            playerTile.setTerrain(null, 4);
        }

        playerTile = tileMap[Player.getLocX()][Player.getLocY()];
        playerTile.setTerrainType(TerrainType.PLAYER, true, 4);

        for (int y = 0; y < DRAW_SIZE; y++) {
            for (int x = DRAW_SIZE - 1; x >= 0; x--) {

                if ((x - y < 27) && (y - x < 23) && (x + y > 28) && (x + y < 68)) {

                    Image[] imageStack = getUpper2DImage(Player.getLocX() + x - (DRAW_SIZE / 2), Player.getLocY() + y - (DRAW_SIZE / 2));
                    if (imageStack == null) {
                        gc.drawImage(TerrainType.SEA.getImage(),
                                (x * (TILE_SIZE / 2)) + (y * (TILE_SIZE / 2)) - (14 * TILE_SIZE),
                                (y * (TILE_SIZE / 4)) - (x * (TILE_SIZE / 4)) + (6 * TILE_SIZE), TILE_SIZE, TILE_SIZE);
                    } else {
                        for (Image image : imageStack) {
                            if (image != null) {
                                gc.drawImage(image,
                                        (x * (TILE_SIZE / 2)) + (y * (TILE_SIZE / 2)) - (14 * TILE_SIZE),
                                        (y * (TILE_SIZE / 4)) - (x * (TILE_SIZE / 4)) + (6 * TILE_SIZE), TILE_SIZE, TILE_SIZE);
                            }
                        }
                    }
                }
            }
        }
    }


    private Image[] getBaseTerrainImage(int x, int y) {

        if (x >= 0 && x < getXlimit() && y >= 0 && y < getYlimit()) {
            return tileMap[x][y].getTerrainImage();
        }
        return null;
    }

    private Image[] getUpper2DImage(int x, int y) {

        if (x >= 0 && x < getXlimit() && y >= 0 && y < getYlimit()) {
            return tileMap[x][y].getUpperImage();
        }
        return null;
    }
}
